package map.gui.util;

import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ModelViewer extends JScrollPane{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static JTextArea text=getTextArea();
	public ModelViewer (String fileName) {
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super(text);
		//JPanel panel= new JPanel();
		try {
			text.setText(getFileContent(fileName));
		} catch (FileNotFoundException e) {	
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.getHorizontalScrollBar().addAdjustmentListener(new MyAction());
		this.getVerticalScrollBar().addAdjustmentListener(new MyAction());

		//this.add(panel,BorderLayout.CENTER);
		//JPanel panel1= new JPanel();
		//this.add(panel1,BorderLayout.SOUTH);
		this.setSize(600,500);
		this.setVisible(true);
	}
	
	

	public ModelViewer (String strTxt, int index) {
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		index=0;
		this.setLayout(new BorderLayout());
		JTextArea text=getTextArea();
		JPanel panel= new JPanel();
		panel.setLayout(new BorderLayout());
		JScrollPane spane;

		
		text.setText(strTxt);
		spane = new JScrollPane(text);
		spane.getHorizontalScrollBar().addAdjustmentListener(new MyAction());
		spane.getVerticalScrollBar().addAdjustmentListener(new MyAction());

		panel.add(spane,BorderLayout.CENTER);
		this.add(panel,BorderLayout.CENTER);
		JPanel panel1= new JPanel();
		this.add(panel1,BorderLayout.SOUTH);
		this.setSize(400,500);
		this.setVisible(true);

	}

	public static JTextArea getTextArea(){
		return new JTextArea("",25,30);
	}
	
	public static String getFileContent(String fileName) throws FileNotFoundException{
		FileReader fr = new FileReader(fileName); 
		BufferedReader br = new BufferedReader(fr);
		String str="";
		String record=null;
		try { 

			   while ( (record=br.readLine()) != null ) {
				   str+=record+"\n";
			   } 
				br.close();
				fr.close();

			} catch (IOException e) { 
			} 
		
		return str;
	}
	public class MyAction implements AdjustmentListener{
		    public void adjustmentValueChanged(AdjustmentEvent ae){
		      //int value = ae.getValue();
		      //System.out.println( Integer.toString(value));
		    }
	}
}
